<?php

function redirect($uri,$convert = true){
    header("Location: ".($convert==true?str_replace('&amp;','&',site_url($uri)):$uri)."");
    exit();
}

function js_redirect($uri,$convert = true){
    echo "<script language=\"javascript\">\n";
    echo "\t window.location.href = '".($convert==true?site_url($uri):$uri)."';";
    echo "</script>\n";
}

function alert($str,$type = 'back',$uri = ''){
    header("Content-Type:text/html; charset=utf-8");
    switch ($type){
        case 'back':
           echo "<script language=\"javascript\">\n";
           echo "\t alert('$str');";
           echo "\t history.back();";
           echo "</script>\n";
           exit();
        break;
        case 'url':
           echo "<script language=\"javascript\">\n";
           echo "\t alert('$str');";
           echo "\t window.location.href = '".site_url($uri)."';";
           echo "</script>\n";
           exit();
        break;
    }
}

function base_url(){
    static $base_url;

    if(!isset($base_url)){
        $basePath = _getScriptPath();
        $base_url = slash_right(str_replace("\\",'/',dirname($basePath)));
    }

    return $base_url;
}

function site_url($uri = ''){
    $url_config =& Loader::config();

    if (is_array($uri)){
        $uri = implode('/', $uri);
    }

    if ($uri == '' || $uri == '/'){
        return base_url().$url_config['index_page'];
    }else{
        $suffix = ($url_config['url_suffix'] == FALSE || strpos($uri,'?')) ? '' : $url_config['url_suffix'];

        if(strtoupper($url_config['uri_protocol']) == 'QUERY_STRING'){
            $uri = str_replace('?','&amp;',$uri);
        }

        return base_url().slash_right($url_config['index_page']).preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
    }
}

function slash_right($pref){
        if ($pref != '' && substr($pref, -1) != '/')
        {
            $pref .= '/';
        }
        return $pref;
}

function _getScriptPath(){
        $filename = basename($_SERVER['SCRIPT_FILENAME']);
        if (basename($_SERVER['SCRIPT_NAME']) === $filename) {
            $baseScript = $_SERVER['SCRIPT_NAME'];
        } elseif (basename($_SERVER['PHP_SELF']) === $filename) {
            $baseScript = $_SERVER['PHP_SELF'];
        } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
            $baseScript = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
        } else {
            // Backtrack up the script_filename to find the portion matching
            // php_self
            $path    = $_SERVER['PHP_SELF'];
            $segs    = explode('/', trim($_SERVER['SCRIPT_FILENAME'], '/'));
            $segs    = array_reverse($segs);
            $index   = 0;
            $last    = count($segs);
            $baseScript = '';
            do {
                $seg     = $segs[$index];
                $baseScript = '/' . $seg . $baseScript;
                ++$index;
            } while (($last > $index) && (false !== ($pos = strpos($path, $baseScript))) && (0 != $pos));
        }
        return $baseScript;
}

function trimArray($Input){
    if (!is_array($Input))
        return trim($Input);

    return array_map('trimArray', $Input);
}

function show404($a){
    @header('Content-Type: text/html; charset=utf-8');
    exit($a);
}

function showError($a){
    @header('Content-Type: text/html; charset=utf-8');
    exit($a);
}